home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / FCOPY.C < prev    next >
C/C++ Source or Header  |  1993-04-26  |  5KB  |  157 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 04-24-93 (14:12)             Number: 98
  4. From: BOB STOUT                    Refer#: 160
  5.   To: MIKE WARDLE                   Recvd: NO  
  6. Subj: File Copy Function             Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.   From an upcoming SNIPPETS:
  9.  
  10. /*
  11. **  FLOPCOPY.C
  12. **
  13. **  Copy a floppy to a hard disk directory with directory recursion
  14. **  Public domain, uses functions from SNIPPETS.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <assert.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23.  
  24. #if defined(__TURBOC__)
  25.  #include <dir.h>
  26.  #include <dos.h>
  27.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  28.  #define _dos_findnext(b) findnext(b)
  29.  #define find_t ffblk
  30.  #define _A_SUBDIR FA_DIREC
  31.  #define attrib ff_attrib
  32.  #define name ff_name
  33. #else
  34.  #include <direct.h>
  35.  #ifdef __ZTC__
  36.   #include <dos.h>
  37.   #ifndef _A_SUBDIR
  38.    #define _A_SUBDIR FA_DIREC
  39.   #endif
  40.  #else                   /* assume MSC/QC                                */
  41.   #include <dos.h>
  42.   #include <errno.h>
  43.   #include <sys\types.h>
  44.  #endif
  45. #endif
  46.  
  47. #include <sys\stat.h>
  48.  
  49. #ifndef SUCCESS
  50.  #define SUCCESS 0
  51. #endif
  52.  
  53. int file_copy(char *,char *);
  54. void do_dir(char *, char *);
  55.  
  56. /*
  57. **  Copy a floppy to an HD subdirectory
  58. */
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62.       char fdrv[3] = "A:";
  63.  
  64.       if (3 > argc)
  65.       {
  66.             puts("Usage: FLOPCOPY drive_letter subdir");
  67.             puts("where: drive_letter is \"A\" or \"B\" (colon optional)");
  68.             puts("       subdir is drive:dir target, e.g. \"C:\\FLOPSTUF\"");
  69.             return EXIT_FAILURE;
  70.       }
  71.       *fdrv = *argv[1];
  72.  
  73.       do_dir(fdrv, argv[2]);
  74. }
  75.  
  76. /*
  77. **  Copy a file (SNIPPETS: Wb_Fcopy.C)
  78. */
  79.  
  80. int file_copy(char *from,char *to)
  81. {       int fdfrom,fdto;
  82.         int bufsiz;
  83.  
  84.         fdfrom = open(from,O_RDONLY|O_BINARY,0);
  85.         if (fdfrom < 0)
  86.                 return 1;
  87.         fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
  88.         if (fdto < 0)
  89.             goto err;
  90.  
  91.         /* Use the largest buffer we can get    */
  92.         for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  93.         {   register char *buffer;
  94.  
  95.             buffer = (char *) malloc(bufsiz);
  96.             if (buffer)
  97.             {   while (1)
  98.                 {   register int n;
  99.  
  100.                     n = read(fdfrom,buffer,bufsiz);
  101.                     if (n == -1)                /* if error             */
  102.                         break;
  103.                     if (n == 0)                 /* if end of file       */
  104.                     {   free(buffer);
  105.                         close(fdto);
  106.                         close(fdfrom);
  107.                         return 0;               /* success              */
  108.                     }
  109.                     if (n != write(fdto,buffer,(unsigned) n))
  110.                         break;
  111.                 }
  112.                 free(buffer);
  113.                 break;
  114.             }
  115.         }
  116. err2:   close(fdto);
  117.         remove(to);                             /* delete any partial file */
  118. err:    close(fdfrom);
  119.         return 1;
  120. }
  121.  
  122. /*
  123. **  Process a directory (SNIPPETS: Treedir.C, modified)
  124. */
  125.  
  126. void do_dir(char *from, char *to)
  127. {
  128.       char search[FILENAME_MAX], new[FILENAME_MAX], newto[FILENAME_MAX];
  129.       struct find_t ff;
  130.  
  131.       strcat(strcpy(search, from), "\\*.*");
  132.       if (SUCCESS == _dos_findfirst(to, 0xff, &ff))
  133.       {
  134.             if (0 == (ff.attrib & _A_SUBDIR))
  135.             {
  136.                   printf("*** %s Exists and is not a directory!\n", to);
  137.                   return;
  138.             }
  139.       }
  140.       else  mkdir(to);
  141.       if (SUCCESS == _dos_findfirst(search, 0xff, &ff)) do
  142.       {
  143.             if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
  144.             {
  145.                   strcat(strcat(strcpy(new, from), "\\"), ff.name);
  146.                   strcat(strcat(strcpy(newto, to), "\\"), ff.name);
  147.                   do_dir(new, newto);
  148.             }
  149.             else
  150.             {
  151.                   char file1[FILENAME_MAX], file2[FILENAME_MAX];
  152.  
  153.                   if ((ff.attrib & (_A_SUBDIR | _A_VOLID)) || '.' == *ff.name)
  154.                         continue;
  155.                   strcat(strcat(strcpy(file1, from), "\\"), ff.name);
  156.                   strcat(strcat(strcpy(file2, to), "\\"), ff.name);
  157.